$(function(){
})


function initSlide() {

}

$('.addCar').each(function(){
	$(this).on("tap",function(){
		var str='<div class="alertBox">'+
				'<div class="title">You have just chosen a product with a  special offer!</div>'+
				'<div class="title">To enjoy the offer:</div>'+
				'<div class="item">'+
					'<div class="info">Buy any product to get another product free</div>'+
					'<p>Choose and place your gift into your shopping cart</p>'+
				'</div>'+
				'<div class="item">'+
					'<div class="info">Buy 1 get 1 free, buy 2 get 1 free and similar offers</div>'+
					'<p>Include the free item in the purchase quantity</p>'+
				'</div>'+
				'<div class="eg">Exanple, if you purchase item A with ‘buy 2 get 1 free’offer, you should place 3 units of item A into shopping cart to enjoy the free gift.</div>'+
		'</div>';
		dialog('Message', str, 1);
	    var $dialog = $('#dialog');
	    $dialog.show();
	    $('#comfirmBtn').html('OK');
	    $dialog.find('.comfirmBtn').one('click', function() {
	        $dialog.hide();
	        $dialog.remove();
	        sessionStorage.removeItem('yh');
			location.href=MPJ.base+"/pages/cart/list.jsp";
	    });
	})
});

$().ready(function () {
	var promo_datas = {};
	var jpdt_data = {};
	var hs_pdt_id = $("#hs_pdt_id").val();
	var hs_promo_no = $("#hs_promo_no").val();

	getProductDetail();
	// 获取产品详情
	function getProductDetail() {
		var data = {
			hf_s_id:'WD11',
			hf_srv_id:'Pv_jpdt_brw',
			hs_action_id:'submit',
			hs_rec_per_page:1,
			hs_srch_page_no:1,
			hs_full_detail:'Y',
			hs_pdt_id:hs_pdt_id
		};
		getAjaxRequest(data, function(result) {
			jpdt_data = result;
			console.log(result);
			if (result.bs_result == BS_RESULT.OK) {
				var bvc_pdt = result.bvc_pdt[0];
				$(".mui-title #pdtName").html(bvc_pdt.bs_desc);

				if (bvc_pdt.bvs_promo_no.length > 0) {
					for (var i = 0; i < bvc_pdt.bvs_promo_no.length; i ++) {
						if (i == 0) {
							getPromotionList(bvc_pdt.bvs_promo_no[i], bvc_pdt.bvs_wd2_prom_text[i]);
						}
						$(".sliderBox .slide-content").append("<div class='slider-item' bvs_promo_no='" + bvc_pdt.bvs_promo_no[i] + "'>" + bvc_pdt.bvs_wd2_prom_text[i] + "</div>");
					}
					initSlide();
				}
			}
		});
	}

	//调用优惠列表方法
	function getPromotionList(item_no, prom_text,pager){
		// $('.preferential p').html(prom_text);
		$("#promo_descript").html(prom_text);
		if (promo_datas[item_no]) {
			randerProduct(promo_datas[item_no]);
			return;
		}

		var data = {
			hs_promo_no:item_no,
			hs_pdt_id: hs_pdt_id,
			hs_rec_per_page:[4,4,4,4,4],
			hs_srch_page_no:[1,1,1,1,1],
			//hs_rec_per_page:"10 10 10 10 10",
			//hs_srch_page_no:pager+" "+pager+" "+pager+" "+pager+" "+pager,
			hs_sort:'',
			hs_full_detail:"Y",
			hs_action_id:"submit",
			hf_s_id:"WD11",
			hf_srv_id:"Pv_jpromo_get",
			hs_call_ts:'',
			button:"submit"
		};

		getAjaxRequest(data, function(result){
			promo_datas[item_no] = result;
			randerProduct(result);
		});
	}

	function randerProduct(result) {
		var objects = setProductList_promotion(result);
		var bc_group_a_pdtHtml = objects[0];
		var bc_group_r_pdtHtml = objects[1];

		if (bc_group_a_pdtHtml) {
			$("#bc_group_a_pdt_div ul").html(bc_group_a_pdtHtml);
		}

		if (bc_group_r_pdtHtml) {
			$("#bc_group_r_pdt ul").html(bc_group_r_pdtHtml);
		}

		bindTapEvent();
	}

	function bindTapEvent() {
		$('.vertical-list li').each(function(){
			var $this=$(this);
			$this.on('tap','.imgBox',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var product_info={};
				product_info.pdt_id=pdt_id;
				product_info.item_no=item_no;
				sessionStorage.setItem('product_info',JSON.stringify(product_info));
				location.href=MPJ.base+"/pages/product/product-detail.jsp";
			})
			$this.on('tap','.addCar',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var arr={
					hf_s_id:"WD11",
					hf_srv_id:"Av_jcart",
					hs_action_id:"add_all_to_cart",
					hvs_add_pdt_id:pdt_id,
					hvs_add_qty:$this.find('.mui-numbox-input').val(),
				};
				getAjaxRequest(arr,function(result){
					if(result.bs_result==BS_RESULT.OK){
						dialog('Message', 'Add to Shopping Cart', 1);
						var $dialog = $('#dialog');
						$dialog.show();
						$dialog.find('.comfirmBtn').one('click', function() {
							$dialog.hide();
							$dialog.remove();
							getCartSumCount();
						})
					}

				});

			})
		})

	}

	var p=1,w,pNum;
	w=$(window).width();
	pNum=$('.sliderBox .slider-item').length;
	if(w>640){
		w=640;
	}
	var bpWin=pNum*w;
	$('.sliderBox .slide-content').css({width:bpWin});
	$('.sliderBox .slide-content .slider-item').css({width:(w-80)});
	$('.slide-content .slider-item').each(function(index){
		$(this).css('left',(index*w));
	})
	$('.sliderBox .slide-focus .mui-number').html('<span>1</span> / '+pNum);

	$('.controll-btn.next').on("tap",function(){
		p=p+1;
		if(p>pNum){
			p=1;
		}
		goPage(p);
	})
	$('.controll-btn.prev').on("tap",function(){
		p=p-1;
		if(p<1){
			p=pNum;
		}
		goPage(p);
	})

	function goPage(p){
		if(p<=pNum){
			$('.sliderBox .slide-content').animate({left:-(w*(p-1))},"slow");
		}else{
			p=1;
			$('.sliderBox .slide-content').animate({left:0},"slow");
		}
		$('.sliderBox .slide-focus .mui-number').html('<span>'+p+'</span> / '+pNum);

		var bvc_pdt = jpdt_data.bvc_pdt[0];
		var bvs_promo_no = bvc_pdt.bvs_promo_no[p - 1]
		var bvs_wd2_prom_text = bvc_pdt.bvs_wd2_prom_text[p - 1]

		getPromotionList(bvs_promo_no, bvs_wd2_prom_text);
	}

});

